package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderMapper;
import com.qf.mapper.DtsOrdersGoodsMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsOrderGoodsService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.DayStatis;
import com.qf.vo.OrderAndUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * projectName:workspace_6
 * author:hyn
 * time:2021/11/24 22:25
 * description:
 */
@Service
public class DtsOrderGoodsServiceImpl implements DtsOrderGoodsService {
    @Autowired
    private DtsOrdersGoodsMapper ordersGoodsMapper;
    @Autowired
    private DtsOrderMapper orderMapper;
    @Autowired
    private StatMapper statMapper;
    @Override
    public List<CategorySellAmts> findOrderCount() {
        return ordersGoodsMapper.findNameAndValue();
    }

    @Override
    public List<OrderAndUser> findOrderUser(int day) {
        List<DayStatis> userAmts = statMapper.findOrderAmts(30);
        List<DtsOrder> orderAndUser = ordersGoodsMapper.findOrderAndUser(day);
        List list1 = new ArrayList<>();
        List<OrderAndUser> list = new ArrayList<>();
//        for (DayStatis userAmt : userAmts) {
            for(int j = 0;j < userAmts.size();j++){
                int i = 0;
                OrderAndUser ou = new OrderAndUser();
                for (DtsOrder orderU : orderAndUser) {
                    if(userAmts.get(j).getDayStr() == null){
                        ou.setDay( list.get(j-1).getDay());
                        ou.setAmount(new BigDecimal(0));
                        ou.setOrders(0);
                        ou.setPcr(new BigDecimal(0));
                        ou.setCustomers(0);
                    }else{
                        if(userAmts.get(j).getDayStr().equals(orderU.getMobile())){
                            if(userAmts.get(j).getAmts() == null){
                                userAmts.get(j).setAmts(new BigDecimal(0));
                                ou.setAmount(new BigDecimal(0));
                            }else{
                                ou.setAmount(userAmts.get(j).getAmts());
                            }
                            ou.setOrders(userAmts.get(j).getCnts());
                            if(orderU.getUserId() != null ){
                                if(list1.contains(orderU.getUserId())){
                                    ou.setCustomers(i);
                                }else{
                                    ou.setCustomers(++i);
                                }
                                    list1.add(orderU.getUserId());
                            }
                            ou.setDay(userAmts.get(j).getDayStr());
                            if(userAmts.get(j).getAmts().compareTo(BigDecimal.ZERO)==0
                                    || i == 0 ){
                                ou.setPcr(new BigDecimal(0));
                            }else{
                                ou.setPcr(userAmts.get(j).getAmts().divide(new BigDecimal(i),2, BigDecimal.ROUND_HALF_UP));
                            }
                        }
                    }
                }
                list.add(ou);
                list1.clear();

        }

        return list;
    }
    @Override
    public IPage<DtsOrder> findAll(String orderSn, String userId, String orderStatusArray, Integer limit, Integer page, String sort, String order) {
        IPage<DtsOrder> ipage = new Page(page,limit);
        QueryWrapper<DtsOrder> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(orderSn)){
            wrapper.like("order_sn",orderSn);
        }
        if(!StringUtils.isEmpty(userId)){
            wrapper.like("user_id",userId);
        }
        if(orderStatusArray != null && orderStatusArray.length() > 0){

            wrapper.in("order_status",orderStatusArray);
        }
        if("asc".equals(order)){
            wrapper.orderByAsc(sort);
        }else{
            wrapper.orderByDesc(sort);
        }
        IPage<DtsOrder> p = orderMapper.selectPage(ipage, wrapper);
        return p;
    }
    @Autowired
    private DtsUserMapper userMapper;
    @Override
    public Object findById(Integer id) {
        DtsOrder order = orderMapper.selectById(id);

        DtsUser user = userMapper.selectById(order.getUserId());
        HashMap<String, Object> users = new HashMap<>();
        users.put("nickname",user.getNickname());
        users.put("avatar",user.getAvatar());
        List<DtsOrderGoods> orderGoods = ordersGoodsMapper.findByOrderId(id);
        HashMap<String, Object> data = new HashMap<>();
        data.put("orderGoods",orderGoods);
        data.put("user",users);
        data.put("order",order);
        return ResponseUtil.ok(data);

    }
}
